﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RestServices;
using RestServices.API;
using RestServices.Response.Bing;
using RestServices.Response.Calendar;
using RestServices.Response.SkyDrive;
using RestServices.Response.Windows;

namespace BingTeste
{
    class Program
    {
        static string BingMapsKey = "Key";
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                SkyDriveService skyDriveService = new SkyDriveService();
                //string locationsRequest = BingService.CreateRequest(BingMapsKey, "New%20York");
                //BingResponse locationsResponse = BingService.MakeRequest(locationsRequest);
                //ProcessResponse(locationsResponse);
                string startToken = "{Token}";
                string refreshToken = "{Refresh Token}";
                WindowsLogin login = null;
                Livelogin form = null;
                LiveLoginService loginService = new LiveLoginService();

                string url1 = loginService.RequestUser(startToken);
                LiveUser user1 = ServiceCall.MakeRequestGet<LiveUser>(url1);

                if (user1 != null)
                {
                    Console.WriteLine("Nome:" + user1.Name);
                    login = new WindowsLogin();
                    login.AccessToken = startToken;
                }
                else
                {
                    WindowsID winId = new WindowsID("{ClientID}", "{ClientSecret}");
                    login = winId.Login(refreshToken, true);
                }

                if (login!=null)
                {
                    //Calendar
                    CalendarService service = new CalendarService();

                    string urlCalendars = service.RequestCalendars(login.AccessToken);
                    CalendarList dataCal = ServiceCall.MakeRequestGet<CalendarList>(urlCalendars);

                    foreach (Calendar cal in dataCal.List)
                    {
                        Console.WriteLine(cal.Name);
                    }

                    
                    //SkyDrive
                    string url = loginService.RequestUser(login.AccessToken);
                    LiveUser user = ServiceCall.MakeRequestGet<LiveUser>(url);
                    Console.WriteLine("Nome:" + user.Name);
                    string urlSky = skyDriveService.RequestFirstFolder(login.AccessToken);
                    SkyDriveFolder folder = ServiceCall.MakeRequestGet<SkyDriveFolder>(urlSky);
                    Console.WriteLine("Pasta:" + folder.Name);

                    string urlSkyList = skyDriveService.RequestFilesFolders(folder.Id, login.AccessToken);
                    SkyDriveFolderList folderList = ServiceCall.MakeRequestGet<SkyDriveFolderList>(urlSkyList);

                    foreach (SkyDriveFolder item in folderList.List)
                    {
                        Console.WriteLine("\tTipo:" + item.Type + " Nome:" + item.Name);
                    }


                    if (1 == 2)
                    {
                        string urlUp = skyDriveService.RequestUpload(folder.Id, login.AccessToken);
                        skyDriveService.UploaderInitialize(urlUp, @"C:\Users\Roberto\Documents\dev\IVP\Video\PrototipoFuncional5.wmv");
                        skyDriveService.Completed += loader_Completed;
                        skyDriveService.Progress += loader_Progress;
                        skyDriveService.Upload();
                        while (!skyDriveService.IsCompleted)
                        {
                        }
                    }

                    //Console.WriteLine("Pasta:" + folder.Name);
                }
                else
                {
                    Console.WriteLine("Num deu!");
                }

                Console.WriteLine("Pressione uma tecla");
                Console.ReadKey();
                //SkyDriveLogin skyResponse = SkyDriveService.MakeRequest(skyDriveRequest);
                // ProcessResponse(locationsResponse);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.Read();
            }

        }

        static void loader_Progress(object sender, System.Net.UploadProgressChangedEventArgs e)
        {
            SkyDriveService loader = (SkyDriveService)sender;
            Console.WriteLine(e.ProgressPercentage.ToString("#99") + " %");
        }

        static void loader_Completed(object sender, System.Net.UploadFileCompletedEventArgs e)
        {
            SkyDriveService loader = (SkyDriveService)sender;
            if (loader.IsCompleted && loader.StatusOk)
                Console.WriteLine("Sucesso");
            else
                Console.WriteLine("Completado com erro");
        }

        static public void ProcessResponse(BingResponse locationsResponse)
        {
            int locNum = locationsResponse.ResourceSets[0].Resources.Length;

            //Get formatted addresses
            //Get all locations in the response and then extract the formatted address for each location
            Console.WriteLine("Show all formatted addresses");
            for (int i = 0; i < locNum; i++)
            {
                BingLocation location = (BingLocation)locationsResponse.ResourceSets[0].Resources[i];
                Console.WriteLine(location.Address.FormattedAddress);
            }
            Console.WriteLine();

            //Get the Geocode Points for each Location
            for (int i = 0; i < locNum; i++)
            {
                BingLocation location = (BingLocation)locationsResponse.ResourceSets[0].Resources[i];
                Console.WriteLine("Geocode Points for " + location.Address.FormattedAddress);
                int geocodePointNum = location.GeocodePoints.Length;
                for (int j = 0; j < geocodePointNum; j++)
                {
                    Console.WriteLine("    Point: " + location.GeocodePoints[j].Coordinates[0].ToString() + "," +
                                                location.GeocodePoints[j].Coordinates[1].ToString());
                    double test = location.GeocodePoints[j].Coordinates[1];
                    Console.Write("    Usage: ");
                    for (int k = 0; k < location.GeocodePoints[j].UsageTypes.Length; k++)
                    {
                        Console.Write(location.GeocodePoints[j].UsageTypes[k].ToString() + " ");
                    }
                    Console.WriteLine("\n\n");
                }
            }
            Console.WriteLine();

            //Get all locations that have a MatchCode=Good and Confidence=High
            Console.WriteLine("Locations that have a Confidence=High");
            for (int i = 0; i < locNum; i++)
            {
                BingLocation location = (BingLocation)locationsResponse.ResourceSets[0].Resources[i];
                if (location.Confidence == "High")
                    Console.WriteLine(location.Address.FormattedAddress);
            }
            Console.WriteLine();

        }
    }
}
